<template>
  <div v-loading="loading" class="AddOrUpdate_main add_container column_one">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <el-form-item label="维保班组编码" prop="code">
          <CodeRuleInput :band-value.sync="formData.code" table-name="device_base_maintenance" column-name="code" :is-edit="id" />
        </el-form-item>
        <el-form-item label="维保班组名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="维保人员" prop="crewIds">
          <el-select v-model="formData.crewIds" filterable multiple placeholder="请选择" clearable collapse-tags>
            <el-option
              v-for="item in userOptions"
              :key="item.userId"
              :label="item.realName"
              :value="item.userId"
              clearable
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="formData.remark"
            type="textarea"
            maxlength="200"
            show-word-limit
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item v-if="isShow" label="状态">
          <el-radio-group v-model="formData.status">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { addOrUpdate, getInfo, userList } from '@/api/equipmentManage/baseInfo/maintenanceTeam'
export default {
  name: 'AddOrUpdate',
  props: {
    layerid: {},
    id: {},
    defaultValue: {}
  },
  data() {
    return {
      loading: true,
      isShow: false,
      formData: {
        id: undefined,
        code: undefined,
        name: undefined,
        userId: undefined,
        roleIdsArray: undefined,
        roleIds: undefined,
        crewIds: [],
        crewNames: undefined,
        remark: undefined,
        status: '1'
      },
      foDate: {
        crewIds: undefined,
        status: 1
      },
      userOptions: [],
      roleOptions: [],
      rules: {
        name: [
          { required: true, message: '维保班组名称不能为空', trigger: 'change' }
        ],
        code: [
          { required: true, message: '维保班组编码不能为空', trigger: 'blur' }
        ],
        crewIds: [
          { required: true, message: '维保人员不能为空', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.getUserList()
  },
  created() {
    if (!this.id) { // 没有id就是新增
      this.loading = false
      Object.keys(this.defaultValue).forEach(key => { // 给右边数据赋值
        this.formData[key] = this.defaultValue[key]
      })
    } else { // 有id就是编辑
      this.getInfo()
    }
  },
  methods: {
    getUserList() {
      userList(this.foDate).then(res => {
        Object.keys(this.formData).forEach(key => {
          this.userOptions = res.data
        })
      })
    },
    getInfo() {
      getInfo(this.id).then(res => {
        Object.keys(this.formData).forEach(key => { // 给右边数据赋值
          this.formData[key] = res.data[key]
        })
        this.formData.status += ''
        this.isShow = true
        this.loading = false
        this.formData.roleIdsArray = this.formData.roleIds.split(',')
      })
    },
    onSubmit() {
      console.log(this.formData.crewIds)
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.formData.roleIds = this.formData.roleIdsArray
          addOrUpdate(this.formData).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$layer.close(this.layerid) // 关闭弹框
            this.$parent.refresh() // 调用父组件的刷新方法
          })
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-select__tags{
  max-width: 100% !important;
}
</style>
